<?php

/*
	Class: Communitas_Integrate_Model_Cart_Api

	About: Author
		Rich Joslin

	About: License
		<http://communit.as/docs/license>
*/
class Communitas_Integrate_Model_Cart_Api extends Mage_Api_Model_Resource_Abstract {

	/* Group: Instance Methods */

	/*
		Function: getItems

		Returns: array
	*/
	public function getItems() {
		// $cart = $this->_getCart();
		// if ($cart->getQuote()->getItemsCount()) {
		// 	$cart->init();
		// 	$cart->save();
		// 	if (!$this->_getQuote()->validateMinimumAmount()) {
		// 		$warning = Mage::getStoreConfig('sales/minimum_order/description');
		// 		$cart->getCheckoutSession()->addNotice($warning);
		// 	}
		// }
		// foreach ($cart->getQuote()->getMessages() as $message) {
		// 	if ($message) {
		// 		$cart->getCheckoutSession()->addMessage($message);
		// 	}
		// }

		// if a customer views their cart, it should be marked as modified
		// because they could have a checkout page open in another window
		// $this->_getSession()->setCartWasUpdated(true);
		try {
			// return $this->_getCart();
			// return $this->_getCart()->getQuote();
			// return $this->_getCart()->getQuote()->getItemsCount();
			// return $this->_getCart()->getQuote()->getItemsCollection();
			return $this->_getCart()->getQuote()->getAllItems();
			// return $this->_getCart()->getQuote()->getAllVisibleItems();
		} catch (Exception $e) {
			return $e;
		}
	}

	public function getCheckoutSessionId() {

		// when hit through the SOAP web service, this is a new session ID every time
		// even when specifying SID=### in the querystring of the WSDL
		return $this->_getSession()->getSessionId();

	}

	/*
		Function: addItem

		Returns: string
	*/
	public function addItem($product_id, $qty) {
		// TODO might need to change the args to one array of params
		// TODO change goBacks to cause a fault... or something
		$cart = $this->_getCart();
		$params = array('product' => $product_id, 'qty' => $qty, 'related_product' => null);
		$product = $this->_initProduct($product_id);
		if (is_null($product)) {
			return "null product";
		}
		$cart->addProduct($product, $params);
		// if (!empty($related_product)) {
		// 	$cart->addProductsByIds(explode(',', $related));
		// }
		$result = $cart->save();
		$this->_getSession()->setCartWasUpdated(true);
		return $result;
		// Mage::dispatchEvent('checkout_cart_add_product_complete', array('product'=>$product, null));
		// $message = $this->__('%s was successfully added to your shopping cart.', $product->getName());
		// if (!$this->_getSession()->getNoCartRedirect(true)) {
		// 	$this->_getSession()->addSuccess($message);
		// }
		// try {
		// }
		// catch (Mage_Core_Exception $e) {
		// 	if ($this->_getSession()->getUseNotice(true)) {
		// 		$this->_getSession()->addNotice($e->getMessage());
		// 	} else {
		// 		$messages = array_unique(explode("\n", $e->getMessage()));
		// 		foreach ($messages as $message) {
		// 			$this->_getSession()->addError($message);
		// 		}
		// 	}
		// 	return "mage exception";
		// }
		// catch (Exception $e) {
		// 	$this->_getSession()->addException($e, $this->__('Can not add item to shopping cart'));
		// 	return "general exception";
		// }
	}

	/* Group: Private or Protected Methods */

	/*
		Function: _getCart

		Returns: Mage_Checkout_Model_Cart
	*/
	protected function _getCart() {
		return Mage::getSingleton('checkout/cart');
	}

	/*
		Function: _getSession

		Returns: Mage_Checkout_Model_Session
	*/
	protected function _getSession() {
		return Mage::getSingleton('checkout/session');
	}

	/*
		Function: _getQuote

		Returns: Mage_Sales_Model_Quote
	*/
	protected function _getQuote() {
		return $this->_getCart()->getQuote();
	}

	/*
		Function: _initProduct

		Returns: Mage_Catalog_Model_Product or boolean false
	*/
	protected function _initProduct($productId) {
		if ($productId) {
			$product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($productId);
			if ($product->getId()) {
				return $product;
			}
		}
		return false;
	}

	/*
		Function: __
			Translate a phrase using Magento methods.

		Returns: string
	*/
	protected function __() {
		$args = func_get_args();
		$expr = new Mage_Core_Model_Translate_Expr(array_shift($args), "integrate");
		array_unshift($args, $expr);
		return Mage::app()->getTranslator()->translate($args);
	}

}
